﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;
using HsTec.Mobile.Green.SqlServerDAL;

public partial class DocumentManage_Search_List : Baimasoft.Web.UI.PageManagerTop
{
    DataFolder MyDF = new DataFolder();
    DataFile datafile = new DataFile();
    Machineroom machineroom = new Machineroom();
    Network MyNt = new Network();
    Area area = new Area();
    Project project = new Project();
    public string sID = "";// "-1";


    protected void Page_Load(object sender, EventArgs e)
    {
        this.BindControlEvent();
        this.HiddenID.Value = "";

        //if (this.Request.Params["ID"] != null)
        //{
        //    sID = this.Request.Params["ID"].Trim();
        //}


        if (!this.IsPostBack)
        {

            ChoiceDt.Checked = true;
            ifChoiceDt.Style.Add("display", "");
            AllDt.Style.Add("display", "none");

            GridView1.Attributes.Add("BorderColor", "LightSkyBlue");
            this.SDT.Text = DateTime.Now.ToString("yyyy-MM") + "-01";
            this.EDT.Text = DateTime.Now.ToString("yyyy-MM-dd");
            ddlRegionDataBind();
            ddlMachineDataBind();
            ddlnetworkDataBind();
            ddlProjectDataBind();
            this.BindGridViewData();//绑定GridView,为删除服务
        }
    }

    protected override void BindGridViewData()
    {
        this.DataSource = datafile.GetDataFileInfos (GetSearchString());
        this.DataSource.DefaultView.Sort = this.SortString;
        GridView1.DataSource = this.DataSource.DefaultView;
        GridView1.DataBind();
        KeepStyle();
    }

    /// <summary>
    /// 区域下拉框
    /// </summary>
    protected void ddlRegionDataBind()
    {
        ddlRegionid.DataSource = area.GetAreaInfos (string.Empty);
        ddlRegionid.DataBind();
        ddlRegionid.Items.Insert(0, "全部");
        ddlRegionid.Items[0].Value = "0";
    }

    /// <summary>
    /// 机房下拉框绑定
    /// </summary>
    protected void ddlMachineDataBind()
    {
        ddlMachineid.DataSource = machineroom.GetMachineRooms ("");
        ddlMachineid.DataBind();
        ddlMachineid.Items.Insert(0, "全部");
        ddlMachineid.Items[0].Value = "0";
    }

    /// <summary>
    /// 网元下拉框绑定
    /// </summary>
    protected void ddlnetworkDataBind()
    {
        ddlnetwork.DataSource = MyNt.GetNetWorks ("");
        ddlnetwork.DataBind();
        ddlnetwork.Items.Insert(0, "全部");
        ddlnetwork.Items[0].Value = "0";
    }

    /// <summary>
    /// 项目下拉框
    /// </summary>
    protected void ddlProjectDataBind()
    {
        this.ddlProject.DataSource = project.GetProjectInfos ("");
        this.ddlProject.DataBind();
        this.ddlProject.Items.Insert(0, "全部");
        this.ddlProject.Items[0].Value = "0";
    }

    protected override string GetSearchString()
    {
        string searchStr = string.Empty;
        searchStr += " (1=1) ";

        if (ChoiceName_1.Value.ToString().Trim() != "")
        {
            if (HChoiceID_1.Value.ToString().Trim() != "")
            {
                searchStr += " and a.FolderID in(" + HChoiceID_1.Value.ToString().Trim() + ") ";
            }
        }
        //else
        //{
        //    searchStr += " and ( a.FolderID = '" + sID + "') ";
        //}

        //if (sID.Trim() != "")
        //{
        //    searchStr += " and ( a.FolderID = '" + sID + "') ";
        //}

        if (this.Name.Text.Trim().Length > 0)
        {
            //searchStr += " and a.Name like '%" + this.Name.Text.ToString().Trim() + "%' ";

            searchStr += " and ( ";
            string[] aFileName = Name.Text.ToString ().Trim ().Split(' ');

            int iFileName = aFileName.Length;
            for (int i = 0; i < iFileName; i++)
            {
                if (i != 0)
                {
                    searchStr += " and a.Name like '%" + aFileName[i].ToString().Trim() + "%'  ";
                }
                else
                {
                    searchStr += " a.Name like '%" + aFileName[i].ToString().Trim() + "%'  ";
                }
            }
            searchStr += " ) ";

        }



        //if (this.KeyWord.Text.Trim().Length > 0)
        //{
        //    searchStr += " and a.KeyWord like '%" + this.KeyWord.ToString().Trim() + "%' ";
        //}

        if (ChoiceDt.Checked == true)
        {
            if (this.SDT.Text.ToString().Trim() != "")
            {
                searchStr += " and convert(varchar(10),a.CreatDT,120) >= '" + this.SDT.Text.ToString().Trim() + "' ";
            }
            else
            {
                searchStr += " and convert(varchar(10),a.CreatDT,120) >= '" + DateTime.Now.ToString("yyyy-MM-dd") + "' ";
            }
            if (this.EDT.Text.ToString().Trim() != "")
            {
                searchStr += " and convert(varchar(10),a.CreatDT,120) <= '" + this.EDT.Text.ToString().Trim() + "' ";
            }
            else
            {
                searchStr += " and convert(varchar(10),a.CreatDT,120) <= '" + DateTime.Now.ToString("yyyy-MM-dd") + "' ";
            }
        }

        string sddlRegionid = ddlRegionid.SelectedValue.ToString().Trim();
        if (sddlRegionid != "0")
        {
            searchStr += " and a.Region ='" + sddlRegionid + "' ";
        }
        if (ddlMachineid.SelectedValue.ToString().Trim() != "0")
        {
            searchStr += " and a.MachineRoom ='" + ddlMachineid.SelectedValue.ToString().Trim() + "' ";
        }
        if (ddlnetwork.SelectedValue.ToString().Trim() != "0")
        {
            searchStr += " and a.NetWork ='" + ddlnetwork.SelectedValue.ToString().Trim() + "' ";
        }
        if (ddlProject.SelectedValue.ToString().Trim() != "0")
        {
            searchStr += " and a.Project ='" + ddlProject.SelectedValue.ToString().Trim() + "' ";
        }

        //取该用户数据角色授权信息
        //string sChoiceFolderCol = AspNetHelper.GetRightFolderIDCol();
        //string sChoiceFileCol = AspNetHelper.GetRightFileIDCol();

        //if ((sChoiceFolderCol.Trim() == "0" || sChoiceFolderCol.Length == 0) && (sChoiceFileCol.Trim() == "0" || sChoiceFileCol.Length == 0))
        //{ //没有授权的情况下
        //    searchStr += " and  ( 1<>1 )";
        //}
        //else
        //{

        //    if (sChoiceFolderCol.Trim() != "0" && sChoiceFolderCol.Length > 0)
        //    {
        //        string sFolderIDCols = MyDF.GetFolderChildIDCols(sChoiceFolderCol.Trim());
        //        if (sFolderIDCols != "")
        //        {
        //            searchStr += " and a.FolderID in(" + sFolderIDCols + ") ";
        //        }
        //    }

        //    if (sChoiceFileCol.Trim() != "0" && sChoiceFileCol.Length > 0)
        //    {
        //        searchStr += " and a.ID in(" + sChoiceFileCol + ") ";
        //    }
        //}

        //if ((sChoiceFolderCol.Trim() == "0" || sChoiceFolderCol.Length == 0) && (sChoiceFileCol.Trim() == "0" || sChoiceFileCol.Length == 0))
        //{ //没有授权的情况下
        //    searchStr += " and  ( 1<>1 )";
        //}

        searchStr += " order by a.name, versionnumber desc ";

        return searchStr;
    }

    protected new void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            if (e.Row.RowIndex % 2 != 0)
            {
                e.Row.Attributes.Add("onclick", "tog(this,'#EFEFF8')");
            }
            else
            {
                e.Row.Attributes.Add("onclick", "tog(this,'#F9F9FB')");
            }
        }
        KeepStyle();
    }

    protected void ddlRegionid_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sddlRegion = ddlRegionid.SelectedValue.ToString().Trim();

        DataTable dt1 = machineroom.GetMachineRooms ("a.region_id='" + sddlRegion + "'");
        this.ddlMachineid.DataValueField = "mroom_id";
        this.ddlMachineid.DataTextField = "mroom_name";
        this.ddlMachineid.DataSource = dt1;
        this.ddlMachineid.DataBind();

        string sddlMachine = ddlMachineid.SelectedValue.ToString().Trim();
        DataTable dt2 = MyNt.GetNetWorks ("a.mroom_id = '" + sddlMachine + "'");
        this.ddlnetwork.DataValueField = "netw_id";
        this.ddlnetwork.DataTextField = "netw_name";
        this.ddlnetwork.DataSource = dt2;
        this.ddlnetwork.DataBind();
        KeepStyle();
    }
    protected void ddlMachineid_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sddlMachine = ddlMachineid.SelectedValue.ToString().Trim();
        DataTable dt2 = MyNt.GetNetWorks ("a.mroom_id = '" + sddlMachine + "'");
        this.ddlnetwork.DataValueField = "netw_id";
        this.ddlnetwork.DataTextField = "netw_name";
        this.ddlnetwork.DataSource = dt2;
        this.ddlnetwork.DataBind();
        KeepStyle();
    }

    /// <summary>
    /// 保存相应的状态
    /// </summary>
    protected void KeepStyle()
    {
        if (ChoiceDt.Checked == true)
        {
            ifChoiceDt.Style.Add("display", "");
            AllDt.Style.Add("display", "none");
            LDt.Value = "";
        }
        else
        {
            ifChoiceDt.Style.Add("display", "none");
            AllDt.Style.Add("display", "");
            LDt.Value = "全部日期";
        }
    }

}
